﻿var options = { items_per_page: 20, link_to: "javascript:;", callback: pageChanged };
var account;
var nickname;
var realname;
var email;
$(function() {
    getValue();
    BindSearchSwitch();
    searchUsers();

    $("#btn_search").click(function() {
        getValue();
        searchUsers();
    });

    $("#btn_add").click(function() {
        $(this).attr("disabled", "disabled").after("<span style=\"color:red;\">提交中.....</span>");
        var uids = "";

        $("input:checked").not("#cb_All").each(function() {
            uids += $(this).val() + ",";
        });

        $.ajax({
            type: "POST",
            url: "../api/access.ashx",
            data: { type: 'addroleusers', roleid: roleid, uids: uids, r: Math.random() },
            dataType: "html",
            success: function(data) {
                $("#btn_delete").next("span").remove();
                if (data == 1) {
                    window.location.href = "roleuser.aspx?id=" + roleid;
                }
            }
        });
    });
});
function getValue() {
    account = $("#txt_account").val();
    nickname = $("#txt_nickname").val();
    realname = $("#txt_realname").val();
    email = $("#txt_email").val();
}
function searchUsers() {
    $.ajax({
        type: "GET",
        url: "../api/user.ashx",
        data: { type: 'usersnum', account: account, nickname: nickname, realname: realname, email: email, r: Math.random() },
        dataType: "string",
        success: function(data) {
            if (data == 0) {
                $(".DG tbody").empty();
                $("#pager").empty();
                return;
            }
            $("#pager").pagination(data, options);
        }
    });
}
function pageChanged(index, jq) {
    $.ajax({
        type: "GET",
        url: "../api/user.ashx",
        data: { type: 'searchuser', account: account, nickname: nickname, realname: realname, email: email, pageindex: index + 1, pagesize: options.items_per_page, r: Math.random() },
        dataType: "json",
        success: function(data) {
            $(".DG tbody").empty();
            $.each(data, function(i, item) {
                $(".DG tbody")
                .append("<tr><td><input type='checkbox' value='" + item.uid + "' /></td>\
                            <td>" + item.row + "</td>\
                            <td>" + item.account + "</td>\
                            <td>" + item.realname + "</td>\
                            <td>" + item.nickname + "</td>\
                            <td>" + item.email + "</td>\
                        </tr>");
            });
            BindCheckBoxEvent($("#btn_add"));
            formReady();
        }
    });
}